<template>
	<view>
		<image src="../../static/logo-5.png" mode="widthFix" class="register"></image>
		<view class="register-container">
			<input type="text" placeholder="账号" class="register-code"  v-model="loginForm.account">
			<input type="text" placeholder="密码" class="register-code"  v-model="loginForm.password">
			<view class="register-desc">
				入职之后您可以从您的留下的个人邮箱中获得对应的账号和密码，如果忘记密码请联系管理的人事进行重置。
			</view>
			<button class="register-btn" @tap="login()">登录系统</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginForm:{
					account: '',
					password:''
				}
			}
		},
		methods: {
			login: function() {
				let that = this;
				//验证账号密码不能为空
				if (that.loginForm.account == null || that.loginForm.account.length == 0) {
					uni.showToast({
						title: '账号不能为空',
						icon: 'none'
					});
					return
				}
				if (that.loginForm.password == null || that.loginForm.password.length == 0) {
					uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					});
					return
				}
				//验证账号和密码格式是否正确
				if(!/^1[3-9]\d{9}$/.test(that.loginForm.account)){
					uni.showToast({
						title: '账号格式不正确',
						icon: 'none'
					});
					return
				}
				if(!/^[a-zA-Z0-9]{4,8}$/.test(that.loginForm.password)){
					uni.showToast({
						title: '密码格式不正确,密码长度为4-8位并且只能是纯数字，纯字母或数字和字母的混合组成!!!',
						icon: 'none'
					});
					return
				}
				//与后端交互获取用户的权限和token
				that.ajax(that.url.login, 'POST', this.loginForm, function(resp) {
					let data = resp.data;
					if(data.code==200000){
						uni.showToast({
							title: data.msg,
							icon: 'none'
						});
					}
					let permission = resp.data.permission;
					let token = resp.data.token;
					//存储对应的权限
					uni.setStorageSync('permission', permission);
					//存储对应的token
					uni.setStorageSync('token', token);
					//存储用户的id
					uni.setStorageSync("all-id",resp.data.id);
					//跳转到工作台页面
					uni.switchTab({
						url: '../index/index'
					})
				});
			}
		}
	}
</script>

<style lang="less">
	@import url("login.less");
</style>
